Solving Linear Congruences

Introduction

When you first learn algebra, one of the most common tasks is solving an equation like $ax = b$.
You look for a number $x$ that makes the equation true.

In modular arithmetic, we face a similar problem, but instead of solving an equation in the usual sense, we solve a congruence: $$ax \equiv b \pmod{n}.$$ This means we want a number $x$ such that when $ax$ is divided by $n$, it leaves the same remainder as $b$.

What a Linear Congruence Is

A linear congruence is a statement of the form: $$ax \equiv b \pmod{n}.$$ This means:

For example:

A congruence is like an equation, but instead of asking for exact equality, we ask for equality up to a remainder.

When a Linear Congruence Has a Solution

A key fact:

A congruence $$ax \equiv b \pmod{n}$$ has a solution if and only if the greatest common divisor of $a$ and $n$ divides $b$.

In simpler terms:

Example:

This simple test saves a lot of time.

Reducing the Congruence

If the greatest common divisor $d$ of $a$ and $n$ also divides $b$, then we can simplify the congruence by dividing everything by $d$: $$ax \equiv b \pmod{n}$$ becomes $$\frac{a}{d}x \equiv \frac{b}{d} \pmod{\frac{n}{d}}.$$ This new congruence is simpler and has the same solutions.

Example:

Solve $4x \equiv 6 \pmod{10}$.

Now we have a simpler congruence with the same solutions.

Using Multiplicative Inverses

Once the congruence is simplified so that $a$ and $n$ are coprime (their greatest common divisor is $1$), we can solve it by finding the multiplicative inverse of $a$ modulo $n$.

The multiplicative inverse of $a$ modulo $n$ is a number $a^{-1}$ such that: $$a \cdot a^{-1} \equiv 1 \pmod{n}.$$ If we can find such a number, then solving $$ax \equiv b \pmod{n}$$ is easy:

Multiply both sides by $a^{-1}$: $$x \equiv a^{-1} b \pmod{n}.$$ Example:

Solve $2x \equiv 3 \pmod{5}$.

Now multiply both sides by $3$: $$x \equiv 3 \cdot 3 \equiv 9 \equiv 4 \pmod{5}.$$ So the solution is $x \equiv 4 \pmod{5}$.

Finding Inverses by Inspection

For small numbers, you can find inverses by trying possibilities:

To find the inverse of $a$ modulo $n$:

Example:

Find the inverse of $7$ modulo $12$.

Try:

So the inverse of $7$ modulo $12$ is $7$.

Finding Inverses Using the Euclidean Algorithm

For larger numbers, guessing becomes slow.
The Euclidean Algorithm gives a systematic way to find inverses.

Solving: $$ax \equiv 1 \pmod{n}$$ It is the same as finding a value for $x$ the linear diophantine equation: $$ax + ny = 1$$

Putting It All Together: Solving Any Linear Congruence

To solve $$ax \equiv b \pmod{n},$$ follow these steps:

  1. Compute $d = \gcd(a,n)$.
  2. If $d$ does not divide $b$, stop — no solution exists.
  3. Divide $a$, $b$, and $n$ by $d$.
  4. Find the inverse of the new $a$ modulo the new $n$.
  5. Multiply the inverse by the new $b$.
  6. That gives one solution; all solutions differ by multiples of the new modulus.

Example:

Solve $6x \equiv 9 \pmod{15}$.

  1. $\gcd(6,15) = 3$, and $3$ divides $9$, so solutions exist.
  2. Divide everything by $3$: $$2x \equiv 3 \pmod{5}.$$
  3. The inverse of $2$ modulo $5$ is $3$.
  4. Multiply: $x \equiv 3 \cdot 3 \equiv 9 \equiv 4 \pmod{5}$.

So the solutions are all numbers of the form: $$x = 4 + 5k,$$

This is equivalent to: $$x \equiv 4 \pmod{5}$$

Calculator

Checking for solvability

  • Given an equation $ax \equiv b \pmod{n}$.
  • It is only solvable if: $\gcd(a, n) \mid b$
divides(gcd(a, n), b) divides(gcd(4, 10), 6)

Solving a congruence

  • Solves an equation $ax \equiv b \pmod{n}$.
  • Returns $m, n$ in the solution: $x \equiv m \pmod{n}$.
solveLinearCongruence(4, 6, 10) solveLinearCongruence(4, 6, 5)

Exercises

Try these to build confidence. Solve each congruence or explain why no solution exists.

  1. Solve $3x \equiv 2 \pmod{7}$.

    Solution

    Solve $3x \equiv 2 \pmod{7}$

    • $\gcd(3,7) = 1$, and $1$ divides $2$, so a solution exists.
    • Find the inverse of $3$ modulo $7$ (a number $k$ with $3k \equiv 1 \pmod{7}$).

    Try small numbers:

    • $3 \cdot 1 = 3$
    • $3 \cdot 2 = 6$
    • $3 \cdot 3 = 9 \equiv 2 \pmod{7}$
    • $3 \cdot 5 = 15 \equiv 1 \pmod{7}$

    So the inverse of $3$ modulo $7$ is $5$.

    Multiply both sides by $5$:

    • $x \equiv 5 \cdot 2 = 10 \equiv 3 \pmod{7}$

    Answer: $x \equiv 3 \pmod{7}$.

  2. Solve $4x \equiv 6 \pmod{10}$.

    Solution

    Solve $4x \equiv 6 \pmod{10}$

    • $\gcd(4,10) = 2$, and $2$ divides $6$, so solutions exist.

    Divide everything by $2$:

    • $4x \equiv 6 \pmod{10}$ becomes $2x \equiv 3 \pmod{5}$.

    Now $\gcd(2,5) = 1$, so we find the inverse of $2$ modulo $5$.

    Try:

    • $2 \cdot 3 = 6 \equiv 1 \pmod{5}$

    So the inverse is $3$.

    Multiply both sides by $3$:

    • $x \equiv 3 \cdot 3 = 9 \equiv 4 \pmod{5}$

    So

    • $x = 4 + 5k$ for any whole number $k$.

    Modulo $10$, the distinct solutions are:

    • $k=0$: $x = 4$
    • $k=1$: $x = 9$
    • $k=2$: $x = 14 \equiv 4 \pmod{10}$ (pattern repeats)

    Answer: $x \equiv 4 \pmod{5}$, i.e. $x \equiv 4$ or $9 \pmod{10}$.

  3. Solve $5x \equiv 1 \pmod{8}$.

    Solution

    Solve $5x \equiv 1 \pmod{8}$

    • $\gcd(5,8) = 1$, so a solution exists.

    Find the inverse of $5$ modulo $8$:

    • $5 \cdot 5 = 25 \equiv 1 \pmod{8}$

    So the inverse of $5$ modulo $8$ is $5$.

    Multiply both sides by $5$:

    • $x \equiv 5 \cdot 1 = 5 \pmod{8}$

    Answer: $x \equiv 5 \pmod{8}$.

  4. Solve $9x \equiv 12 \pmod{15}$.

    Solution

    Solve $9x \equiv 12 \pmod{15}$

    • $\gcd(9,15) = 3$, and $3$ divides $12$, so solutions exist.

    Divide everything by $3$:

    • $9x \equiv 12 \pmod{15}$ becomes $3x \equiv 4 \pmod{5}$.

    Now $\gcd(3,5) = 1$, so find the inverse of $3$ modulo $5$:

    • $3 \cdot 2 = 6 \equiv 1 \pmod{5}$

    So the inverse is $2$.

    Multiply both sides by $2$:

    • $x \equiv 2 \cdot 4 = 8 \equiv 3 \pmod{5}$

    So

    • $x = 3 + 5k$ for any whole number $k$.

    Modulo $15$, the distinct solutions are:

    • $k=0$: $x = 3$
    • $k=1$: $x = 8$
    • $k=2$: $x = 13$
    • $k=3$: $x = 18 \equiv 3 \pmod{15}$ (pattern repeats)

    Answer: $x \equiv 3 \pmod{5}$, i.e. $x \equiv 3, 8, 13 \pmod{15}$.

  5. Solve $7x \equiv 3 \pmod{11}$.

    Solution

    Solve $7x \equiv 3 \pmod{11}$

    • $\gcd(7,11) = 1$, so a solution exists.

    Find the inverse of $7$ modulo $11$:

    Try:

    • $7 \cdot 8 = 56 \equiv 1 \pmod{11}$

    So the inverse is $8$.

    Multiply both sides by $8$:

    • $x \equiv 8 \cdot 3 = 24 \equiv 2 \pmod{11}$

    Answer: $x \equiv 2 \pmod{11}$.

  6. Determine whether $6x \equiv 5 \pmod{9}$ has a solution.

    Solution

    Determine whether $6x \equiv 5 \pmod{9}$ has a solution

    • $\gcd(6,9) = 3$.

    Check whether $3$ divides $5$:

    • It does not (since $5/3$ is not a whole number).

    So the congruence has no solution.

    Answer: No solution; $3$ does not divide $5$.

  7. Solve $8x \equiv 4 \pmod{12}$.

    Solution

    Solve $8x \equiv 4 \pmod{12}$

    • $\gcd(8,12) = 4$, and $4$ divides $4$, so solutions exist.

    Divide everything by $4$:

    • $8x \equiv 4 \pmod{12}$ becomes $2x \equiv 1 \pmod{3}$.

    Now $\gcd(2,3) = 1$, so find the inverse of $2$ modulo $3$:

    • $2 \cdot 2 = 4 \equiv 1 \pmod{3}$

    So the inverse is $2$.

    Multiply both sides by $2$:

    • $x \equiv 2 \cdot 1 = 2 \pmod{3}$

    So

    • $x = 2 + 3k$ for any whole number $k$.

    Modulo $12$, the distinct solutions are:

    • $k=0$: $x = 2$
    • $k=1$: $x = 5$
    • $k=2$: $x = 8$
    • $k=3$: $x = 11$
    • $k=4$: $x = 14 \equiv 2 \pmod{12}$ (pattern repeats)

    Answer: $x \equiv 2 \pmod{3}$, i.e. $x \equiv 2, 5, 8, 11 \pmod{12}$.

  8. Find all solutions to $10x \equiv 25 \pmod{35}$.

    Solution

    Find all solutions to $10x \equiv 25 \pmod{35}$

    • $\gcd(10,35) = 5$, and $5$ divides $25$, so solutions exist.

    Divide everything by $5$:

    • $10x \equiv 25 \pmod{35}$ becomes $2x \equiv 5 \pmod{7}$.

    Now $\gcd(2,7) = 1$, so find the inverse of $2$ modulo $7$:

    • $2 \cdot 4 = 8 \equiv 1 \pmod{7}$

    So the inverse is $4$.

    Multiply both sides by $4$:

    • $x \equiv 4 \cdot 5 = 20 \equiv 6 \pmod{7}$

    So

    • $x = 6 + 7k$ for any whole number $k$.

    Modulo $35$, these are:

    • $k=0$: $x = 6$
    • $k=1$: $x = 13$
    • $k=2$: $x = 20$
    • $k=3$: $x = 27$
    • $k=4$: $x = 34$
    • $k=5$: $x = 41 \equiv 6 \pmod{35}$ (pattern repeats)

    Answer: $x \equiv 6 \pmod{7}$, i.e. all numbers of the form $6 + 7k$.

  9. Solve $2x \equiv 7 \pmod{9}$.

    Solution

    Solve $2x \equiv 7 \pmod{9}$

    • $\gcd(2,9) = 1$, so a solution exists.

    Find the inverse of $2$ modulo $9$:

    • $2 \cdot 5 = 10 \equiv 1 \pmod{9}$

    So the inverse is $5$.

    Multiply both sides by $5$:

    • $x \equiv 5 \cdot 7 = 35 \pmod{9}$

    Reduce $35$ modulo $9$:

    • $35 - 27 = 8$, so $35 \equiv 8 \pmod{9}$.

    Thus:

    • $x \equiv 8 \pmod{9}$

    Answer: $x \equiv 8 \pmod{9}$.

  10. Solve $11x \equiv 8 \pmod{13}$.

    Solution

    Solve $11x \equiv 8 \pmod{13}$

    • $\gcd(11,13) = 1$, so a solution exists.

    Notice $11 \equiv -2 \pmod{13}$, so rewrite:

    • $11x \equiv 8 \pmod{13}$ becomes $-2x \equiv 8 \pmod{13}$.

    Multiply both sides by $-1$:

    • $2x \equiv -8 \pmod{13}$.

    Now $-8 \equiv 5 \pmod{13}$ (since $13 - 8 = 5$), so:

    • $2x \equiv 5 \pmod{13}$.

    Find the inverse of $2$ modulo $13$:

    • $2 \cdot 7 = 14 \equiv 1 \pmod{13}$

    So the inverse is $7$.

    Multiply both sides by $7$:

    • $x \equiv 7 \cdot 5 = 35 \pmod{13}$

    Reduce $35$ modulo $13$:

    • $35 - 26 = 9$, so $35 \equiv 9 \pmod{13}$.

    Thus:

    • $x \equiv 9 \pmod{13}$

    Answer: $x \equiv 9 \pmod{13}$.